import React, {Component} from 'react';
import {Button, Card, List, message, Radio} from "antd";
import './notice.less'
import { getUserOrders, deleteOrder} from "../../api/order";
import { deleteLease} from "../../api/lease";
import { addPrice } from "../../api/user";
import { connect } from 'react-redux'

const mapStateToProps = (state,props) => {
    return {
        userInfoNumber: state.user.info.number,
        userInfoId: state.user.info.id,
        userInfo: state.user.info
    }
}

@connect(mapStateToProps)
class Notice extends Component {
    constructor(props) {
        super(props);
        this.state = {
            orders: []
        }
    }

    init = () => {
        getUserOrders({userId: this.props.userInfoId}).then(res => {
            this.setState({
                orders: res.data
            })
        })
    }

    delete = (item) => {
        if (item.isPass){
            deleteLease(item).then(res => {
                message.success(res.msg);
            }).catch(err => {
                console.log(err)
            })
            deleteOrder(item).then(res => {
                this.init();
            }).catch(err => {
                console.log(err)
            })
        }else{
            addPrice({number: this.props.userInfoNumber,money: item.money}).then(res => {
                console.log(res.msg);
            })
            deleteOrder(item).then(res => {
                message.success(res.msg);
                this.init();
            }).catch(err => {
                console.log(err)
            })
        }
    }

    componentDidMount() {
        this.init()
    }

    render() {
        return (
            <div className='notice'>
                <Card title='订单通知'>
                    <List
                        itemLayout="horizontal"
                        dataSource={this.state.orders}
                        renderItem={(item,index) => {
                            return <List.Item key={index}>
                                <List.Item.Meta
                                    title="租赁单"
                                    description={<div><span className='listSpan'>汽车编号：{item.carId}</span><span className='listSpan'>租车时间：{item.carId}小时</span><span>租金：{item.money}</span></div>}
                                />
                                {
                                    <Radio.Group>
                                        <span>{item.isPass ? '': '等待审核中'}</span>
                                        <Button onClick={this.delete.bind(this,item)}>{item.isPass ? '还车': '取消订单'}</Button>
                                    </Radio.Group>
                                }
                            </List.Item>
                        }}
                    />
                </Card>
            </div>
        );
    }
}

export default Notice;